/**
 * @file Driver_ADC.c
 * @brief 使用了连续转换模式，配合DMA，不断读取
 * @author 2bam 
 * @version 1.0
 * @date 2024-12-01
 * @copyright Copyright (c) 2024  NULL
 * 
 * @change log:
 * Date           Version Author   Describe
 * 2024-12-01     1.0     2bam     first version
 */


/* Includes -------------------------------------------------------------------*/
#include "config.h"
#include "adc.h"
#include "font.h"
#include "rtc.h"
#include "main.h"
#include "rtthread.h"
#include "TJCScreen.h"

/* define ---------------------------------------------------------------------*/
#define ADC_CHANNEL_NUM 3
/* typedef --------------------------------------------------------------------*/

/* variables ------------------------------------------------------------------*/
uint32_t adc_value[ADC_CHANNEL_NUM] = {0};    /** 用于存储三个通道的ADC值 */
struct rt_thread adc_thread;
static uint8_t adc_stack[4000];
/* Private function prototypes ------------------------------------------------*/
static void ShowCpuTemperature(void *para);


/**
 * @brief 启动ADC转换，搭配DMA使用
 * @date 2024-12-01
 * @author 2bam 
 */
static int ADCThreadInit(void)
{
    rt_err_t ret;

    /** 初始化显示线程 2024-12-15 Sunday 01:36:16 */
    ret = rt_thread_init(&adc_thread,
                        "adc thread",
                        ShowCpuTemperature, NULL,
                        adc_stack, sizeof(adc_stack),
                        RT_THREAD_PRIORITY_ADC, 5);
    rt_thread_startup(&adc_thread);
    return ret;
}
INIT_APP_EXPORT(ADCThreadInit);

/**
 * @brief 显示CPU温度和参考电压
 * @date 2024-12-01
 * @author 2bam 
 */
static void ShowCpuTemperature(void *para)
{
    float cpu_temp = 0.0;
    float voltage = 0.0;
    float rotation = 0.0;
    char message[50] = {0};

    /** 开启ADC采集 2024-12-15 Sunday 01:36:02 */
    HAL_ADCEx_Calibration_Start(&hadc1);
    HAL_ADC_Start_DMA(&hadc1,adc_value,sizeof(adc_value)/sizeof(uint32_t));

    while (1)
    {
        cpu_temp = (1.43f - adc_value[0]*3.3f/4095)*1000/4.3f + 25.0;
        voltage = adc_value[1]*3.3f/4095;
        rotation = adc_value[2]*3.3f/4095;
        //printf("cpu temp:%.2f\n",cpu_temp);
        //printf("voltage ref:%.2f\n",voltage);
        //printf("rotation:%.2f\n",rotation);
    
        /** 使用OLED显示 2024-12-01 Sunday 19:20:41 */
        sprintf(message,"cpu temp:%.2f C",cpu_temp);
        //OLED_PrintASCIIString(0,0,message,&afont8x6,OLED_COLOR_NORMAL);
        sprintf(message,"voltage ref:%.2fV",voltage);
        //OLED_PrintASCIIString(0,8,message,&afont8x6,OLED_COLOR_NORMAL);
        sprintf(message,"rotation:%.2fV",rotation);
        //OLED_PrintASCIIString(0,16,message,&afont8x6,OLED_COLOR_NORMAL);

        rt_thread_mdelay(1000);
        
    }
}





